let Vue
import createMacher, { pathMap } from './createMacher'
import HASHHistory from './history/hash'
import BrowerHistory from './history/history'
import routerLink from './components/router-link'
import routerView from './components/router-view'
class VueRouter{
  constructor(options) {
    this.beforeEachHooks=[]
    let routes = options.routes || []
    this.macher = createMacher(routes)
    let mode = options.mode || 'hash'
    if (mode =='hash') {
      this.history = new HASHHistory(this)
    } else if (mode == 'history') {
      this.history = new BrowerHistory(this)
    }
    console.log(this.history.routes,623000);
  }
  match(location) {
    return this.macher.match(location)
  }
  push(location) {
    this.history.push(location)
  }
  beforeEach(cb) {
    this.beforeEachHooks.push(cb)
  }
  init(app) {
    this.history.transitionTo(this.history.getCurrentLocation(), () => {
      this.history.setupListener()
    })
    //每次路由切换的时候都需要调用listen方法中的回调函数实现更新
    this.history.listen((newRoute) => {
      app._route = newRoute
    })
  }
}
VueRouter.install = function (_Vue) {
  Vue = _Vue
  Vue.mixin({
    beforeCreate() {
      if (this.$options.router) {
        this._routerRoot = this
        this._router = this.$options.router
        this._router.init(this)  //this指向new Vue
        Vue.util.defineReactive(this, "_route", this._router.history.current)
      } else {
        this._routerRoot = this.$parent && this.$parent._routerRoot
      }
    },
  })
  Object.defineProperty(Vue.prototype, '$router', {
    get() {
      return this._routerRoot && this._routerRoot._router
    }
  })
  Object.defineProperty(Vue.prototype, '$route', {
    get() {
      return this._routerRoot && this._routerRoot._route
    }
  })
  Vue.component("router-link", routerLink)
  Vue.component("router-view", routerView)
}
export default VueRouter
//53:37秒